<template>
  <view class="pass-container">
    <!-- 返回按钮 -->

    <view class="custom-nav-bar">
      <!-- 返回按钮 -->
      <view class="nav-back" @click="goBack">
        <image src="/static/img/back.png" mode=""></image>
      </view>
      <!-- 页面标题 -->
      <view class="nav-title"></view>
    </view>

    <!-- 标题 -->
    <view class="title">修改密码</view>

    <!-- 表单 -->
    <view class="form-container">
      <view class="form-item">
        <input
          class="input-field"
          type="password"
          v-model="oldPassword"
          placeholder="请输入旧密码" />
      </view>

      <view class="form-item">
        <input
          class="input-field"
          type="password"
          v-model="newPassword"
          placeholder="请输入新密码" />
      </view>

      <view class="form-item">
        <input
          class="input-field"
          type="password"
          v-model="confirmPassword"
          placeholder="新密码确认" />
      </view>
    </view>

    <!-- 确认按钮 -->
    <button class="confirm-btn" :disabled="!isFormValid" @click="handleSubmit">
      确认
    </button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      oldPassword: "",
      newPassword: "",
      confirmPassword: "",
    };
  },
  computed: {
    isFormValid() {
      return this.oldPassword && this.newPassword && this.confirmPassword;
    },
  },
  methods: {
    goBack() {
      uni.navigateBack({ delta: 1 });
    },
    handleSubmit() {
      // 表单校验
      if (!this.oldPassword) {
        uni.showToast({
          title: "请输入旧密码",
          icon: "none",
        });
        return;
      }

      if (!this.newPassword) {
        uni.showToast({
          title: "请输入新密码",
          icon: "none",
        });
        return;
      }

      if (this.newPassword.length < 6) {
        uni.showToast({
          title: "新密码不能少于6位",
          icon: "none",
        });
        return;
      }

      if (this.newPassword !== this.confirmPassword) {
        uni.showToast({
          title: "两次输入的密码不一致",
          icon: "none",
        });
        return;
      }

      // 这里添加修改密码的API调用
      console.log("提交修改密码", {
        oldPassword: this.oldPassword,
        newPassword: this.newPassword,
      });

      this.$Api.user
        .getmakepass({
          password: this.oldPassword,
          new_password: this.newPassword,
        })
        .then((res) => {
          uni.showToast({
            title: "密码修改成功",
            icon: "success",
          });
          // 修改成功后可以返回或跳转到其他页面
          setTimeout(() => {
            uni.reLaunch({
              url: "/pages/login/login",
            });
          }, 1500);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.pass-container {
  padding: 20px;
  background-color: #f0f7ff;
  min-height: 100vh;
  .custom-nav-bar {
    height: 50px; /* 导航栏高度 */
    display: flex;
    align-items: center;
    color: #000;
  }
  .nav-back {
    flex: 0 0 auto;
    cursor: pointer;
    image {
      width: 12px;
      height: 24px;
      // padding-left: 16px;
    }
  }
  .nav-title {
    flex: 1 1 auto;
    text-align: center;
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 17px;
    color: #000000;
  }

  .back-icon {
    font-size: 24px;
    color: #333;
    margin-bottom: 30px;
  }

  .title {
    font-size: 24px;
    font-weight: bold;
    color: #333;
    margin-bottom: 40px;
    margin-top: 20px;
  }

  .form-container {
    margin-bottom: 40px;
  }

  .form-item {
    margin-bottom: 20px;
  }

  .input-field {
    width: 100%;
    height: 50px;
    border-bottom: 1px solid #e0e0e0;
    font-size: 16px;
    padding: 10px 0;
  }

  .confirm-btn {
    background-color: #2979ff;
    color: white;
    font-size: 18px;
    height: 50px;
    line-height: 50px;
    border-radius: 5px;
    text-align: center;
    margin-top: 40px;
  }

  .confirm-btn[disabled] {
    background-color: #a0cfff;
  }
}
</style>
